package com.xy.blog.core.converter.mapstruct.config;

import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * @author zanhonglei
 * @version V1.0
 * @Description: mapstruct 格式化
 * 参考 https://mapstruct.org/documentation/stable/reference/html/
 * @date 2020/3/14
 */
@Component
public class ObjectWrapperMapper {

    public static final String FORMATTER_PATTERN = "yyyy-MM-dd HH:mm:ss";

    public String asString(Date date) {
        return date != null ? new SimpleDateFormat( FORMATTER_PATTERN )
                .format( date ) : "";
    }

    public String asString(LocalDateTime date) {
        if (date != null) {
            return date.format(DateTimeFormatter.ofPattern(FORMATTER_PATTERN));
        }
        return "";
    }

    public String asString(Long item) {
        if (item != null) {
            return String.valueOf(item);
        }
        return null;
    }

}